package com.xpalive.base.valid.adapter;

import com.xpalive.base.valid.BizValid;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import org.springframework.util.StringUtils;

public class BizValidator implements ConstraintValidator<BizValid, Object> {

    private String defaultMessage;

    @Override
    public void initialize(BizValid bizValid) {
        this.defaultMessage = bizValid.message();
    }

    @Override
    public boolean isValid(Object o, ConstraintValidatorContext context) {
        context.disableDefaultConstraintViolation();
        String messageTemplate = context.getDefaultConstraintMessageTemplate();
        messageTemplate = StringUtils.isEmpty(messageTemplate) ? defaultMessage : messageTemplate;
        context.buildConstraintViolationWithTemplate("test")
            .addConstraintViolation();
        return false;
    }
}
